package com.jm.weibo.logic;

import java.util.HashMap;
import java.util.List;

import weibo4andriod.User;

//import weibo4andriod.User;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.jm.weibo.ui.R;

public class FansAdapter extends  BaseAdapter{
   
	private Context context;
	
	private List<User> fans;
	
	private LayoutInflater inflater;
	
	private FansHodler fansHodler;
	
	public FansAdapter(Context context, List<User> friends) {
		super();
		this.context = context;
		this.fans = friends;
        this.fansHodler = new FansHodler();
		this.inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return this.fans.size() + 2;
	}

	@Override
	public Object getItem(int position) {
		if (position == 0){
			return null;
		}else if (position == this.getCount() - 1){
			return null;
		}
		// 返回这一条用户信息
		else{
			return this.fans.get(position - 1);
		}
	}

	@Override
	public long getItemId(int position) {
		if (position == 0){// 刷新
			return 0;
		} else if (position > 0 && position < this.getCount() - 1){// 返回微博的ID
			return this.fans.get(position - 1).getId();
		} else { // 选中了更多
			return -1;
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
//		if(convertView == null){
//			convertView = this.inflater.inflate(R.layout.item_attention, null);
//		}
//		
		if(position == 0){ //刷新
			View userItem = LayoutInflater.from(context).inflate(R.layout.moreitemsview, null);
			TextView tv = (TextView) userItem.findViewById(R.id.tvItemContent);
			tv.setText("刷新");
			return userItem;
		}else if(position == this.getCount() - 1){ //更多
			View userItem = LayoutInflater.from(context).inflate(
					R.layout.moreitemsview, null);
			TextView tv = (TextView) userItem.findViewById(R.id.tvItemContent);
			tv.setText("更多");
			return userItem;
		}else{
			View userItem = null;
			if (convertView != null && convertView.findViewById(R.id.tvItemName) != null) {
				Log.d("getview", "doGetView-------get TextView-----------"
						+ position);
				userItem = convertView;
			}else {
				Log.d("getview", "doGetView-------new TextView-----------"+ position);
				// 把xml布局文件变成View对象
				userItem = this.inflater.inflate(R.layout.item_fans, null);
			}
			this.initUserHoler(userItem);
			User user = this.fans.get(position-1);
			try {
				this.fansHodler.tvUsername.setText(user.getName());
				this.fansHodler.tvDesctption.setText(user.getDescription());
				this.fansHodler.btnAttentionState.setText("取消关注");
				
				if (MainService.mainService.allIcon.get(user.getId()) != null) {
					this.fansHodler.ivUserIcon.setBackgroundDrawable(MainService.mainService.allIcon.get(user.getId()));
				} else {
					this.fansHodler.ivUserIcon.setBackgroundResource(R.drawable.portrait);
					 HashMap param=new HashMap();
			         param.put("uid", user.getId());
			         param.put("url", user.getProfileImageURL());
			         Log.d("user", user.getId()+"-----" +user.getProfileImageURL());
			    	 Task ts=new Task(Task.TASK_GET_USER_IMAGE_ICON_FANS,param); 
			    	 MainService.addNewTask(ts);
				}
			} catch (Exception e) {
				e.printStackTrace();
				Log.e("ERROR", "加载好友出错了");
			}
			return userItem;	
		}
		
		
	}
    
	private void initUserHoler(View userItem) {
		this.fansHodler.tvUsername = (TextView) userItem.findViewById(R.id.tvUsername);
		this.fansHodler.tvDesctption = (TextView) userItem.findViewById(R.id.tvDesctption);
		this.fansHodler.ivUserIcon =  (ImageView) userItem.findViewById(R.id.ivUserIcon);
		this.fansHodler.btnAttentionState = (Button) userItem.findViewById(R.id.ivAttentionState);
	}

	/**
	 * 动态给AttentionAdapter添加数据
	 * @param datas
	 */
	public void addMoreData(List<User> datas){
		this.fans.addAll(datas);
		this.notifyDataSetChanged();
	}
}
